package com.reader.core.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.security.SecureRandom;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

/**
 * @author liujianqing
 * @date 2018/10/25
 */
public class IdUtils {
	
	private static final String POSSIBLE_CHARS="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ=";
	
	private static final String DIGIT_UPPERALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
		
	public static final long DIFF_TIMESTAMP = 1000000000000L;
	private static long l = 0L;

	
	/**
	 * 生成长度为length的随机序列
	 * 
	 * @param length
	 * @return
	 */
	public static String getUniqueFileName(int length) {
		
		StringBuilder sb = new StringBuilder(length);
		SecureRandom random = new SecureRandom();
		
		for (int i = 0; i < length; i++) {
			sb.append(POSSIBLE_CHARS.charAt(random.nextInt(POSSIBLE_CHARS.length())));
		}
		sb.append("==").append(IntUtil.c10to32(System.currentTimeMillis() - DIFF_TIMESTAMP));
		
		return sb.toString();
	}
	
	private static String getRandStr(int length){
		
		StringBuilder sb = new StringBuilder(length);
		
		SecureRandom random = new SecureRandom();
		
		for (int i = 0; i < length; i++) {
			sb.append(DIGIT_UPPERALPHABET.charAt(random.nextInt(DIGIT_UPPERALPHABET.length())));
		}
		
		return sb.toString();
	}
	
	public static String getRandDigit(int length){
		
		StringBuilder sb = new StringBuilder(length);
		
		SecureRandom random = new SecureRandom();
		
		for (int i = 0; i < length; i++) {
			sb.append(DIGIT_UPPERALPHABET.charAt(random.nextInt(10)));
		}
		
		return sb.toString();
	}
	
	public static String getUid(int uid) {
		
		return IntUtil.c10to32(2163000 + uid);
	}
	
	public static int getUid(String idStr) {
		
		return new Long(IntUtil.c32to10(idStr) - 2163000).intValue();
	}
	
	public static String getOrderNO(int orderid, int userid) {
		Date date = new Date();
		
		long time = date.getTime();
		
		String orderStr = String.valueOf(time/680 + orderid);
		String userStr = getUid(userid);
		
		return  StringUtils.substring(userStr, -1) + StringUtils.substring(orderStr, 0, 5) + getRandDigit(1) + StringUtils.substring(orderStr, 5);
	}
	
	public static String generateUUID() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString();
	}
	
	public static String generateQrCode() {
		UUID uuid = UUID.randomUUID();
		return IntUtil.c10to32(System.currentTimeMillis() - DIFF_TIMESTAMP) + "-" + uuid.toString();
	}
	
	/**
	 * 字符串生成0-1之间的小数
	 * @param str
	 * @return
	 */
	public static double genDecimal(String str){
		Integer hash=Math.abs(str.hashCode());
		String tmp="0."+ StringUtils.reverse(hash.toString());
		return Double.parseDouble(tmp);
	}
	
	/**
	 * 创建id号，每毫秒一个
	 * 
	 * add synchronized by xicheng 2015-02-06
	 * 
	 * @return
	 */
	public static synchronized String getid() {
		try {
			long longtime = System.currentTimeMillis();
			if (longtime > l) {
				l = longtime;
			} else {
				l++;
			}
			return IntUtil.c10to32(l - DIFF_TIMESTAMP);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	public static String getPId(int id){
		return "pg_"+id;
	}
	
	public static int getPId(String pId){
		
		if (StringUtils.isBlank(pId)) {
			return -1;
		}
		if (StringUtils.isNumeric(pId)) {
			return -1;
		}
		else {
			return NumberUtils.toInt(StringUtils.substringAfter(pId, "pg_") ,-1);
		}
	}
	
	public static String getStdPId(int id){
		return "stdPg_"+id;
	}
	
	public static int getStdPId(String pId){
		
		if (StringUtils.isBlank(pId)) {
			return -1;
		}
		if (StringUtils.isNumeric(pId)) {
			return -1;
		}
		else {
			return NumberUtils.toInt(StringUtils.substringAfter(pId, "stdPg_") ,-1);
		}
	}
	
	public static String getOrdPgId(int id){
		return "ordPg_"+id;
	}
	
	public static int getOrdPgId(String pId){
		
		if (StringUtils.isBlank(pId)) {
			return -1;
		}
		if (StringUtils.isNumeric(pId)) {
			return -1;
		}
		else {
			return NumberUtils.toInt(StringUtils.substringAfter(pId, "ordPg_") ,-1);
		}
	}
	
	public static String getPackId(int id){
		return "pack_"+id;
	}
	
	public static int getPackId(String packId){
		
		if (StringUtils.isBlank(packId)) {
			return -1;
		}
		if (StringUtils.isNumeric(packId)) {
			return -1;
		}
		else {
			return NumberUtils.toInt(StringUtils.substringAfter(packId, "pack_") ,-1);
		}
	}
	
	public static String getImgId(int id){
		return "img_"+id;
	}
	
	public static int getImgId(String imgId){
		
		if (StringUtils.isBlank(imgId)) {
			return -1;
		}
		if (StringUtils.isNumeric(imgId)) {
			return -1;
		}
		else {
			return NumberUtils.toInt(StringUtils.substringAfter(imgId, "img_") ,-1);
		}
	}
	
	public static String getShareEncodeMsgId(int msgId){
		return ("SHAREKEY" + IdUtils.getUid(msgId).toLowerCase());
	}
	
	public static int getMsgIdFromShareEncode(String msgIdStr){
	
		return IdUtils.getUid(StringUtils.substringAfter(msgIdStr, "SHAREKEY").toUpperCase());
	}
	
	public static void main(String[] args) {
		System.out.println("begin...");
		HashMap<String,String> idmap = new HashMap<>();
		for (int i = 0; i < 10000000; i++) {
			String yxId = getUid(i);
			String yxlower = yxId.toLowerCase();
			if (!idmap.containsKey(yxlower)) {
				idmap.put(yxlower, yxlower);
			}
			else {
				System.out.println(i + " : " + yxId);
			}
		}
		System.out.println("set size : " + idmap.size());
		System.out.println("... end");
		System.out.println(idmap.containsKey("2209P"));
		System.out.println(idmap.containsKey("2209p"));
		System.out.println(getUid("2209p"));
		System.out.println(getUid("220bm"));
		System.out.println(getUid(920));
		System.out.println(getUid("2216G"));
	}

}